package fr.emse.tatiana.analysis.annotation.actions;


import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IFolder;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.jface.action.Action;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.jface.window.Window;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.FileDialog;
import org.eclipse.ui.IEditorInput;
import org.eclipse.ui.IWorkbenchPage;
import org.eclipse.ui.IWorkbenchWindow;
import org.eclipse.ui.PartInitException;

import fr.emse.tatiana.Activator;
import fr.emse.tatiana.CorpusNavigationView;
import fr.emse.tatiana.analysis.annotation.categorisation.CategorisationAnalysisInput;
import fr.emse.tatiana.commands.ICommandIds;
import fr.emse.tatiana.corpus.Corpus;
import fr.emse.tatiana.display.BadDisplayFile;
import fr.emse.tatiana.replayable.ReplayableInput;
import fr.emse.tatiana.utils.WSFileDialog;

/**
 * Creates a categorisation analysis based on an existing categories description
 * 
 * @author dyke
 *
 */
public class NewCategFromFileAnalysisAction extends Action {
	private final IWorkbenchWindow window;
	private final String navigationID;
	private final String editorID;
	
	private static NewCategFromFileAnalysisAction instance;
	
	public static NewCategFromFileAnalysisAction getInstance() {
		return instance;
	}

	public NewCategFromFileAnalysisAction(IWorkbenchWindow window, String label, String edid, String navid) {
		this.window = window;
		this.navigationID = navid;
		this.editorID = edid;
		setText(label);
		// The id is used to refer to the action in a menu or toolbar
		setId(ICommandIds.CMD_NEW_CATEGORISATION_FROM_FILE_ANALYSIS);
		// Associate the action with a pre-defined command, to allow key bindings.
		//setActionDefinitionId(ICommandIds.CMD_NEW_CATEGORISATION_FROM_FILE_ANALYSIS);
		setImageDescriptor(fr.emse.tatiana.Activator.getImageDescriptor("/icons/sample2.gif"));
		this.setEnabled(false);
		instance = this;
	}


	public void run() {
		if(window != null) {	
			IProject project = ResourcesPlugin.getWorkspace().getRoot().getProject("Tatiana");
			IFolder filters = project.getFolder("categories");
			WSFileDialog dialog = new WSFileDialog(
					window.getShell(), 
					SWT.SINGLE, 
					"Choose a categorisation file to create analysis",
					filters,
					false,
					new String[]{"xml"},
					null); 

			if (dialog.open()!=Window.OK) {
				return;
			}
			IProject p = ((CorpusNavigationView) window.getActivePage().findView(this.navigationID)).getSelectedCorpus().getProject();
			IEditorInput input = new CategorisationAnalysisInput(p,(IFile) dialog.getSingleResult());
			try {
				window.getActivePage().openEditor(input, editorID);
			} catch (PartInitException e) {
				new MessageDialog(window.getShell(), 
						"Problem Opening Categorisation Analysis",
						null,
						"Could not open categorisation " + input.getName(),
						MessageDialog.ERROR,
						new String[]{"Ok"},
						0).open();
			}
		}
	}

}
